<?php

class ProfilePasswordForm extends Users {
	public $oldPassword;
	public $newPassword;
	public $verifyPassword;
	
	public function rules() {
		$rules = array(
			array('oldPassword, newPassword, verifyPassword', 'required'),
			array('oldPassword, newPassword, verifyPassword', 'filter', 'filter'=>'trim'),
			array('oldPassword', 'checkOldPassword'),
			array('oldPassword, newPassword, verifyPassword', 'length', 'max'=>128, 'min' => 4,'message' =>UserModule::t('Неверные параметры. Длина пароля должна быть больше 4 символов.')),
			array('verifyPassword', 'compare', 'compareAttribute'=>'newPassword', 'message' => UserModule::t("Не совпадает.")),
		);
		return $rules;
	}
	
	public function attributeLabels() {
		return array(
			'oldPassword' => UserModule::t('Старый пароль'),
			'newPassword' => UserModule::t('Новый пароль'),
			'verifyPassword' => UserModule::t('Новый пароль еще раз'),
		);
	}	
	
	public function checkOldPassword($attribute,$params) {
		if ($this->hasErrors()) return false;
		//if ($this->password != md5($this->oldPassword)) {
		if ($this->password !== crypt($this->oldPassword, $this->password)) {
			$this->addError("oldPassword", UserModule::t('Старый пароль введен неверно.'));
			return false;
		}
		return true;
	}	
    
    public function beforeSave() {
		$this->password = crypt($this->newPassword, Controller::blowfishSalt());
        return parent::beforeSave();
    }      
}